package com.zhangwei.controller;


import com.zhangwei.pojo.HotCity;
import com.zhangwei.pojo.HotCity;
import com.zhangwei.pojo.bean.ResultVO;
import com.zhangwei.service.HotCityService;
import com.zhangwei.utils.ResultVoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manage/hotcity")
@Slf4j
public class HotCityMController {   //注意  xml文件引起的报错

    @Autowired
    HotCityService hotCityService;

    @RequestMapping("/modify")  // 修改ui
    public ModelAndView modify(@RequestParam(value = "id",required = false) int id, Map<String,Object> map){
        //map是传给模板的参数，这是一种写法
//        System.out.println("access index11111111111111111");

        if(!StringUtils.isEmpty(id)){
            HotCity hotCity=hotCityService.selectById(id);
            map.put("hotCity",hotCity);
        }
        return new ModelAndView("/hotcity/modify",map);

    }

    @RequestMapping("/addui")  // 增加ui
    public ModelAndView addui(Map<String,Object> map){
        //map是传给模板的参数，这是一种写法
//        System.out.println("access index11111111111111111");
        return new ModelAndView("/hotcity/add",map);

    }

    @RequestMapping("/list")
//    @ResponseBody   //注意是否返回模板
    public ModelAndView list(){
//        List<HotCity> list= HotCityService.selectAll();
//        return ResultVoUtil.success(list);
        List<HotCity> list= hotCityService.selectAll();
        ModelAndView mav=new ModelAndView("/hotcity/list");
        mav.addObject("list",list);  //第二种传值方式
        log.info("获取热门城市列表成功：{}",list);
        return mav;
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResultVO add(@RequestParam(value = "cityName") String cityName,//对应form的name
                        @RequestParam(value = "wcnCityNo") String wcnCityNo,
                        @RequestParam(value = "status") String status,
                        @RequestParam(value = "description") String description){  //参数为pojo  key-value都要用引号postman在body写 raw-json

        System.out.println(cityName+"--"+wcnCityNo+"--"+status+"--"+description);
        HotCity hotCity=new HotCity();
        hotCity.setCityName(cityName);
        hotCity.setWcnCityNo(wcnCityNo);
        hotCity.setStatus(status);
        hotCity.setDescription(description);

        int res= hotCityService.insertOne(hotCity);
        if(res>0){
            log.info("新增热门城市列表成功：{}",hotCity);
            return ResultVoUtil.success();
        }
        return ResultVoUtil.error(1,"新增失败");
    }

    //https://blog.csdn.net/suki_rong/article/details/80445880
//    @RequestMapping("/add")
//    @ResponseBody
//    public ResultVO add(@RequestBody HotCity HotCity){  //参数为pojo  key-value都要用引号postman在body写 raw-json
//
//        log.info("access add");
//        System.out.println(HotCity.toString());
//        int res= HotCityService.insertOne(HotCity);
//        if(res>0){
//            return ResultVoUtil.success();
//        }
//        return ResultVoUtil.error(1,"插入失败");
//    }

    // BindingResult bindingResult注意这个参数，如果在参数列表中添加这个参数会报错？？用处？
    @RequestMapping("/update")
    @ResponseBody
    public ResultVO update(@RequestParam(value = "id") int id,   //不写会err
                           @RequestParam(value = "cityName") String cityName,//对应form的name
                           @RequestParam(value = "wcnCityNo") String wcnCityNo,
                           @RequestParam(value = "status") String status,
                           @RequestParam(value = "description") String description){

//        System.out.println("form:"+form.getCkey());
        HotCity hotCity=new HotCity();
        hotCity.setId(id);
        hotCity.setCityName(cityName);
        hotCity.setWcnCityNo(wcnCityNo);
        hotCity.setStatus(status);
        hotCity.setDescription(description);
        int res=hotCityService.updateOne(hotCity);
        if(res>0){
            return ResultVoUtil.success();
        }
        return ResultVoUtil.error(1,"更新失败");

    }

    /**
     * ok  postman:body-raw-son  键值对json字符串  可正常测试
     * @param configid
     * @return
     */
//    @RequestMapping("/update")
//    @ResponseBody
//    public ResultVO update(@RequestBody HotCity HotCity){
//
//        int res=HotCityService.updateOne(HotCity);
//        if(res>0){
//            return ResultVoUtil.success();
//        }
//        return ResultVoUtil.error(1,"更新失败");
//
//    }

    @RequestMapping("/del")
    @ResponseBody
    public ResultVO update(@RequestParam(value = "id") int configid){

        int res=hotCityService.delById(configid);
        if(res>0){
            return ResultVoUtil.success();
        }
        return ResultVoUtil.error(1,"删除失败");

    }

    @RequestMapping("/test")
    @ResponseBody
    public String test(){


        return "test ok";

    }

    @RequestMapping("/testftl")
    @ResponseBody
    public String test2(ModelMap map){
        List<HotCity> list= hotCityService.selectAll();

        map.addAttribute("list",list);  //第二种传值方式

        return "/hotcity/list";

    }



}
